package ${groupId}.server.service;

import ${groupId}.server.pojo.${Domain};
import ${groupId}.server.dto.${Domain}Dto;
import ${groupId}.server.pojo.${Domain}Example;
import ${groupId}.server.dto.PageDto;
import ${groupId}.server.mapper.${Domain}Mapper;
import ${groupId}.server.utils.CopyUtil;
import ${groupId}.server.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;
<#list typeSet as type>
    <#if type=='Date'>
import java.util.Date;
    </#if>
</#list>
/**
* @author: duck
* @date: 2020-09-17 19:41
* @desc:${tableNameCn} 服务类
**/
@Service
public class ${Domain}Service {
    @Resource
    private ${Domain}Mapper ${domain}Mapper;

    /**
    * 获取${tableNameCn}列表
    * @param pageDto
    */
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        ${Domain}Example ${domain}Example=new ${Domain}Example();
        <#list fieldList as field>
            <#if field.nameHump=='sort'>
                ${domain}Example.setOrderByClause("sort asc");
            </#if>
        </#list>
        List<${Domain}> ${domain}List = ${domain}Mapper.selectByExample(${domain}Example);
        PageInfo<${Domain}>pageInfo=new PageInfo<>(${domain}List);
        pageDto.setTotal(pageInfo.getTotal());
        List<${Domain}Dto> ${domain}DtoList = CopyUtil.copyList(${domain}List,${Domain}Dto.class);
        pageDto.setList(${domain}DtoList);
    }

    /**
    * 保存${tableNameCn}信息
    * @param ${domain}Dto
    */
    public void save(${Domain}Dto ${domain}Dto){
        ${Domain} ${domain}= CopyUtil.copy(${domain}Dto,${Domain}.class);
        if(StringUtils.isEmpty(${domain}Dto.getId())){
        this.insert(${domain});
        }else {
        this.update(${domain});
        }
    }

    /**
    * 新增
    * @param ${domain}
    */
    private void insert(${Domain} ${domain}){
        <#list typeSet as type>
            <#if type=='Date'>
        Date now=new Date();
            </#if>
        </#list>
        <#list fieldList as fieId>
            <#if fieId.nameHump=='createdAt'>
        ${domain}.setCreatedAt(now);
            </#if>
            <#if fieId.nameHump=='updatedAt'>
        ${domain}.setUpdatedAt(now);
            </#if>
            <#if fieId.nameHump=='id'&&fieId.type=='String'>
        ${domain}.setId(UuidUtil.getShortUuid());
            </#if>
        </#list>
        ${domain}Mapper.insert(${domain});
    }

    /**
    * 更新
    * @param ${domain}
    */
    private void update(${Domain} ${domain}){
        <#list fieldList as fieId>
            <#if fieId.nameHump=='createdAt'>
         ${domain}.setUpdatedAt(new Date());
            </#if>
        </#list>
        ${domain}Mapper.updateByPrimaryKey(${domain});
    }

    /**
    * 删除${tableNameCn}信息
    * @param id
    */
    <#list fieldList as fieId>
        <#if fieId.nameHump=='id'&&fieId.javaType=='String'>
     public void delete(String id){
        ${domain}Mapper.deleteByPrimaryKey(id);
     }
        </#if>
        <#if fieId.nameHump=='id'&&fieId.javaType=='Integer'>
     public void delete(Integer id){
        ${domain}Mapper.deleteByPrimaryKey(id);
     }
        </#if>
        <#if fieId.nameHump=='id'&&fieId.javaType=='Long'>
     public void delete(Long id){
        ${domain}Mapper.deleteByPrimaryKey(id);
     }
        </#if>

    </#list>

}
